package com.ruoyi.web.controller.campus;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyInformation;
import com.ruoyi.campus.service.IMyInformationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 华软资讯Controller
 *
 * @author zht
 * @date 2022-01-14
 */
@RestController
@RequestMapping("/campus/information")
public class MyInformationController extends BaseController
{
    @Autowired
    private IMyInformationService myInformationService;

    /**
     * 查询华软资讯列表
     */
    @PreAuthorize("@ss.hasPermi('campus:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyInformation myInformation)
    {
        startPage();
        List<MyInformation> list = myInformationService.selectMyInformationList(myInformation);
        return getDataTable(list);
    }

    /**
     * 查询资讯(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/listForWx")
    public TableDataInfo listForWx(MyInformation myInformation)
    {
        startPage();
        myInformation.setNewsStatus("1");
        List<MyInformation> list = myInformationService.selectMyInformationList(myInformation);
        return getDataTable(list);
    }

    /**
     * 根据imtId获取资讯详情信息
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getDetailByImtId")
    public Map<String,Object> getDetailByImtId(@RequestParam("imtId") String imtId){
        Map<String,Object> detail = myInformationService.getDetailByImtId(imtId);
        String content = detail.get("content").toString();
        String newContent = content.replace("/dev-api", "http://localhost:8088");
        detail.put("content",newContent);
        return detail;
    }

    /**
     * 查询Top3资讯
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getTopThreeInformation")
    public List<Map<String,Object>> getTopThreeInformation(){
        List<Map<String, Object>> list = myInformationService.selectTopThreeInformation();
        for (Map<String, Object> map : list) {
            String image = map.get("image").toString();
            String newImage = "http://localhost:8088"+image;
            map.put("image",newImage);
        }
        return list;
    }

    /**
     * 导出华软资讯列表
     */
    @PreAuthorize("@ss.hasPermi('campus:information:export')")
    @Log(title = "华软资讯", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyInformation myInformation)
    {
        List<MyInformation> list = myInformationService.selectMyInformationList(myInformation);
        ExcelUtil<MyInformation> util = new ExcelUtil<MyInformation>(MyInformation.class);
        return util.exportExcel(list, "华软资讯数据");
    }

    /**
     * 获取华软资讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:information:query')")
    @GetMapping(value = "/{imtId}")
    public AjaxResult getInfo(@PathVariable("imtId") String imtId)
    {
        return AjaxResult.success(myInformationService.selectMyInformationByImtId(imtId));
    }

    /**
     * 新增华软资讯
     */
    @PreAuthorize("@ss.hasPermi('campus:information:add')")
    @Log(title = "华软资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyInformation myInformation)
    {
        myInformation.setImtId(UUID.randomUUID().toString().replace("-",""));
        return toAjax(myInformationService.insertMyInformation(myInformation));
    }

    /**
     * 修改华软资讯
     */
    @PreAuthorize("@ss.hasPermi('campus:information:edit')")
    @Log(title = "华软资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyInformation myInformation)
    {
        return toAjax(myInformationService.updateMyInformation(myInformation));
    }

    /**
     * 删除华软资讯
     */
    @PreAuthorize("@ss.hasPermi('campus:information:remove')")
    @Log(title = "华软资讯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{imtIds}")
    public AjaxResult remove(@PathVariable String[] imtIds)
    {
        return toAjax(myInformationService.deleteMyInformationByImtIds(imtIds));
    }
}
